﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace QL_Nha_Sach_Phuong_Nam.Quản_lý_nhà_cung_cấp.Quản_lý_phiếu_nhập
{
    public partial class FormPhieuNhap : Form
    {
        private CXulyPhieuNhap xulyPhieuNhap;
        private Quản_lý_sách.Quản_lý_thông_tin_sách.CXulythongtinsach xulySach;
        private Quản_lý_nhà_cung_cấp.Quản_lý_thông_tin_nhà_cung_cấp.CXulyNhacungcap xulyNCC;
        private double tong=0;
        private int iphantram = 0;

        public FormPhieuNhap()
        {
            InitializeComponent();
        }

        private void FormPhieuNhap_Load(object sender, EventArgs e)
        {
            xulyPhieuNhap = new CXulyPhieuNhap();
            xulySach = new Quản_lý_sách.Quản_lý_thông_tin_sách.CXulythongtinsach();
            xulyNCC = new Quản_lý_thông_tin_nhà_cung_cấp.CXulyNhacungcap();

            cbbNCC.DataSource = xulyNCC.GetdsNhaCungCap();
            cbbNCC.DisplayMember = "NhaCungCapName";
            cbbNCC.ValueMember = "NhaCungCapID";
            hienthidsPhieuNhap(xulyPhieuNhap.getdsPhieuNhap());
        }

        #region cac ham chuc nang

        //hien thi ds sach len combobox tronng dgv
        private void hienthidssach(List<Kết_nối.Sach> sach, int dong)
        {
            DataGridViewComboBoxCell c = (DataGridViewComboBoxCell)dgvChiTiet.Rows[dong].Cells[0];
            c.DataSource = sach;
            c.DisplayMember = "SachNo";
            c.ValueMember = "SachNo";
        }

        //Truoc khi chon sach
        private void dgvChiTiet_CellEnter(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            switch (e.ColumnIndex)
            {
                case 0:
                    List<Kết_nối.Sach> ds = xulySach.getdsSach();
                    hienthidssach(ds, e.RowIndex);
                    break;
            }
        }

        //chon tac sach
        private void dgvChiTiet_CellValueChanged(object sender, DataGridViewCellEventArgs e)
        {
            if (e.RowIndex < 0) return;
            switch (e.ColumnIndex)
            {
                case 0:
                    DataGridViewComboBoxCell c = (DataGridViewComboBoxCell)dgvChiTiet.Rows[e.RowIndex].Cells[0];
                    string masach = c.Value.ToString();
                    Kết_nối.Sach sach = xulySach.TimSachtheoma(masach);
                    if (sach != null)
                        hienthisach(sach, e.RowIndex);
                    break;
                case 4:
                    double x = double.Parse(dgvChiTiet.Rows[e.RowIndex].Cells[4].Value.ToString());
                    int y = int.Parse(dgvChiTiet.Rows[e.RowIndex].Cells[3].Value.ToString());
                    dgvChiTiet.Rows[e.RowIndex].Cells[5].Value = x * y;
                    tong += double.Parse(dgvChiTiet.Rows[e.RowIndex].Cells[5].Value.ToString());
                    txtTong.Text = (tong * (100 - iphantram) / 100).ToString();
                    break;

            }
        }

        //hien thi sach len dgv
        private void hienthisach(Kết_nối.Sach sach, int dong)
        {
            dgvChiTiet.Rows[dong].Cells[1].Value = sach.SachName;
            dgvChiTiet.Rows[dong].Cells[3].Value = '0';
            dgvChiTiet.Rows[dong].Cells[4].Value = '0';
        }

        //hien thi ds phieu nhap
        private void hienthidsPhieuNhap(List<Kết_nối.PhieuNhap> ds_phieu)
        {
            liv_ListPhieu.Items.Clear();
            if (ds_phieu != null)
            {
                foreach (Kết_nối.PhieuNhap phieu in ds_phieu)
                {
                    if (phieu.PhieuNhapStatus == "live")
                    {
                        ListViewItem lvi = liv_ListPhieu.Items.Add(phieu.PhieuNhapNo);
                        lvi.SubItems.Add(phieu.PhieuNhapDate.ToString());
                    }
                }
            }
        }

        //hien thi phieu nhap
        private void hienthiphieunhap(Kết_nối.PhieuNhap phieu)
        {
            dgvChiTiet.Rows.Clear();
            txtMaPhieu.Text = phieu.PhieuNhapNo;
            txtDes.Text = phieu.PhieuNhapDescription;
            cbbNCC.Text = phieu.NhaCungCap.NhaCungCapName;
            dtpThoiGian.Value = phieu.PhieuNhapDate.Value;
            foreach (Kết_nối.ChiTietPhieuNhap ctp in phieu.ChiTietPhieuNhaps)
            {
                int i = dgvChiTiet.Rows.Add();
                hienthidssach(xulySach.getdsSach(), i);
                dgvChiTiet.Rows[i].Cells[0].Value = ctp.Sach.SachNo;
                dgvChiTiet.Rows[i].Cells[1].Value = ctp.Sach.SachName;
                dgvChiTiet.Rows[i].Cells[3].Value = ctp.ChiTietPhieuNhapQuantum;
                dgvChiTiet.Rows[i].Cells[4].Value = ctp.ChiTietPhieuNhapPrice;
                dgvChiTiet.Rows[i].Cells[5].Value = ctp.ChiTietPhieuNhapQuantum * ctp.ChiTietPhieuNhapPrice;
                txtTong.Text = ctp.ChiTietPhieuNhapTotal.ToString();
            }
        }

        private void liv_ListPhieu_SelectedIndexChanged(object sender, EventArgs e)
        {
            foreach (ListViewItem lvi in liv_ListPhieu.SelectedItems)
            {
                string ma = lvi.SubItems[0].Text;
                Kết_nối.PhieuNhap phieu = xulyPhieuNhap.TimPhieuNhapTheoMa(ma).First();
                if (phieu != null)
                {
                    hienthiphieunhap(phieu);
                }
                break;
            }
        }

        private void cleardulieu()
        {
            txtMaPhieu.Text = "";
            txtDes.Text = "";
            cbbNCC.SelectedIndex = 1;
            dtpThoiGian.Value = DateTime.Now;
            dgvChiTiet.Rows.Clear();
            txtTong.Text = "0";
        }
        
        private void btnThem_Click(object sender, EventArgs e)
        {

            Kết_nối.PhieuNhap phieu = new Kết_nối.PhieuNhap();
            phieu.PhieuNhapID = xulyPhieuNhap.getdsPhieuNhap().Last().PhieuNhapID + 1;
            phieu.PhieuNhapNo = txtMaPhieu.Text;
            phieu.PhieuNhapDescription = txtDes.Text;
            phieu.NhaCungCap = xulyNCC.TimNhacungcapma(cbbNCC.Text);
            phieu.PhieuNhapDate = dtpThoiGian.Value;
            phieu.PhieuNhapStatus = "live";
            foreach (DataGridViewRow r in dgvChiTiet.Rows)
            {
                if (r.IsNewRow) continue;
                Kết_nối.ChiTietPhieuNhap ctp = new Kết_nối.ChiTietPhieuNhap();
                ctp.ChiTietPhieuNhapQuantum = int.Parse(r.Cells[3].Value.ToString());
                ctp.ChiTietPhieuNhapPrice = double.Parse(r.Cells[4].Value.ToString());
                ctp.ChiTietPhieuNhapTotal = double.Parse(txtTong.Text);
                string ma = r.Cells[0].Value.ToString();
                ctp.Sach = xulySach.TimSachtheoma(ma);
                phieu.ChiTietPhieuNhaps.Add(ctp);
            }
            xulyPhieuNhap.ThemPhieuNhap(phieu);
            cleardulieu();
            hienthidsPhieuNhap(xulyPhieuNhap.getdsPhieuNhap());
        }

        private void ccbPhanTram_SelectedIndexChanged(object sender, EventArgs e)
        {
            int x = int.Parse(cbbPhanTram.Text);
            double y = (tong * (100 - x)) / 100;
            iphantram = x;
            txtTong.Text = y.ToString();
        }

        private void btnTim_Click(object sender, EventArgs e)
        {
            if (txtMa.Text == "")
                hienthidsPhieuNhap(xulyPhieuNhap.getdsPhieuNhap());
            else
                hienthidsPhieuNhap(xulyPhieuNhap.TimPhieuNhapTheoMa(txtMa.Text));
        }

        private void btnSua_Click(object sender, EventArgs e)
        {

        }
        #endregion

        

        
    }
}
